<?php
class ThongbaosController extends AppController {

	var $name = 'Thongbaos';

	function index($nhanvien_id) {
		$this->Thongbao->recursive = 0;
		$this->set('thongbaos', $this->paginate(array('nhanvien_id'=>$nhanvien_id)));
        $this->set(compact('nhanvien_id'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thongbao', $this->Thongbao->read(null, $id));
	}

	function add($nhanvien_id=null) {
		if (!empty($this->data)) {
			$this->Thongbao->create();
            $chucdanh = $this->data['Thongbao']['chucdanh'];
            foreach ($chucdanh as $value){
                $record['Thongbao']['nhanvien_id']=$nhanvien_id;
                $record['Thongbao']['chucdanh_id']=$value;
                $record['Thongbao']['ngaytao']=$this->data['Thongbao']['ngaytao'];
                $record['Thongbao']['noidung']=$this->data['Thongbao']['noidung'];
                $record['Thongbao']['active']=$this->data['Thongbao']['xem'];
                $this->Thongbao->saveAll($record);
            }
			if ($this->Thongbao->save($record)) {
				$this->Session->setFlash(__('Thông báo đã được lưu', true));
				$this->redirect(array('controller'=>'z1thongbaos','action' => 'index',$nhanvien_id));
			} else {
				$this->Session->setFlash(__('Thông báo không thể lưu', true));
			}
		}
		$chucdanhs = $this->Thongbao->Chucdanh->find('list',array('fields'=>array('Chucdanh.id','Chucdanh.chucdanh')));
        //debug($chucdanhs);
		$nhanviens = $this->Thongbao->Nhanvien->find('list');
		$this->set(compact('roles', 'chucdanhs','nhanvien_id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid thongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Thongbao->save($this->data)) {
				$this->Session->setFlash(__('The thongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thongbao could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Thongbao->read(null, $id);
		}
		$roles = $this->Thongbao->Role->find('list');
		$nhanviens = $this->Thongbao->Nhanvien->find('list');
		$this->set(compact('roles', 'nhanviens'));
	}

	function delete($id = null,$nhanvien_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for thongbao', true));
			$this->redirect(array('controller'=>'z1thongbaos','action'=>'index',$nhanvien_id));
		}
		if ($this->Thongbao->delete($id)) {
			$this->Session->setFlash(__('Thông báo đã xóa', true));
			$this->redirect(array('controller'=>'z1thongbaos','action'=>'index',$nhanvien_id));
		}
		$this->Session->setFlash(__('Thông báo không thể xóa', true));
		$this->redirect(array('controller'=>'z1thongbaos','action' => 'index',$nhanvien_id));
	}
}
